using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using UnityEngine;
/// <summary>
/// 选股器
/// </summary>
public class StocksPicker : BatchProcessor
{
    public int dataFromYear = StockDataAnalysis.defaultFromYear;
    public float goUpRate = 0.04f;

    private List<StockData> pickedStocks;
    
    [ContextMenu("FindStocksWithZljlcAndLowHslAndGoUpTop")]
    public void FindStocksWithZljlcAndLowHslAndGoUpTop()
    {
        pickedStocks = new List<StockData>();
        actionInterval = 0.01f;
        DoActionToAllStocks(FindStockWithZljlcAndLowHslAndGoUpTop);
    }

    void FindStockWithZljlcAndLowHslAndGoUpTop(StockData stock)
    {
        if (CheckIfZljlcAndLowHslAndGoUp(stock.dm, goUpRate))
        {  
            pickedStocks.Add(stock);
        }
    }

    protected override void onCompleted()
    {
        string content = $"筛选出股票：{JsonConvert.SerializeObject(pickedStocks)}";
        Debug.LogError(content);
        onEnd?.Invoke(content);
    }
     //IEnumerator IEFindStocksWithZljlcAndLowHslAndGoUpTop()
    //  void DoFindStockWithZljlcAndLowHslAndGoUpTop()
    // {
    //     if (m_curIndex >= allStocks.Count)
    //     {
    //         onPickStockEnd?.Invoke();
    //         return;
    //     }
    //
    //     var stock = allStocks[m_curIndex];
    //    
    //     //if(i%10 == 0)
    //     //   yield return null;
    //     m_curIndex++;
    //     DoActionDelay.DelayAction(0.01f, DoFindStockWithZljlcAndLowHslAndGoUpTop);
    //     onPickStockProgress?.Invoke((float)m_curIndex/allStocks.Count);
    // }

    /// <summary>
    /// 判断股票是否近三日主力净流出且换手率低于该股波段开涨的平均换手率
    /// </summary>
    /// <returns></returns>
    bool CheckIfZljlcAndLowHslAndGoUp(string stockCode, float goUpRate)
    {
        var stockAllData = StocksDataController.instance.GetStockAllData(stockCode);
        if (stockAllData == null || stockAllData.stockJieDuanZhuLi == null 
            || stockAllData.stockJieDuanZhuLi.Count <= 0 )
            return false;
        
        var lastTrans = stockAllData.stockTransitions[^1];
        var beforeLast = stockAllData.stockTransitions[^2];
        double up = lastTrans.c - beforeLast.c;
        double upRate = up / beforeLast.c;

        if (upRate < goUpRate) // 0)
            return false;

        Debug.Log($"[{stockCode}] 涨幅:{upRate}");

        var lastDayZhuli = stockAllData.stockJieDuanZhuLi[0];

        string path = StocksDataController.stocksStatisticPath(dataFromYear, stockCode);
        if (!File.Exists(path))
            return false;
        
        var ss = JsonConvert.DeserializeObject<StockStatistic>(File.ReadAllText(path));
        if (ss == null)
            return false;
        
        if (lastDayZhuli.jlr3 < 0 && lastTrans.hs < ss.averHslOnBeginGoUp)
        {
            Debug.LogError(
                $"found stock:{stockCode}, jlrl3:{lastDayZhuli.jlrl3},{lastTrans.hs}, averHsl:{ss.averHsl},averHslOnBeginGoUp:{ss.averHslOnBeginGoUp}");
            return true;
        }
        else
        {
            return false;
        }
    }

}